package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * ClassName: DishController
 * Package: com.sky.controller.admin
 * Description:
 *
 * @Author 龙培
 * @Create 2025/3/2 14:45
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品,{}",dishDTO);
        dishService.saveWithFlavors(dishDTO);
        //清除缓存
        cleanCache("dish_" + dishDTO.getCategoryId());
        return Result.success();
    }

    /**
     * 分页码查询菜品列表
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品列表")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {//注意同过url传过来的参数不需要使用@RequestBody注解
        log.info("分页查询菜品列表,{}",dishPageQueryDTO);
        PageResult page = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("批量删除菜品,{}",ids);
        dishService.deleteBatch(ids);
        cleanCache("dish_*"); //逻辑太复杂，直接全部删除
        return Result.success();
    }

    /**
     * 根据id查询菜品详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品详情")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品详情,{}",id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品,{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);
        cleanCache("dish_*"); //如果修改分类，删除逻辑太复杂，直接全部删除
        return Result.success();
    }

    /**
     * 根据分类id查询菜品列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("跟据分类id查询菜品列表）")
    public Result<List<Dish>> list(Long categoryId) {
        log.info("根据分类id查询菜品列表");
        List<Dish> dishList = dishService.SelectListById(categoryId);
        return Result.success(dishList);
    }

    /**
     * 停售/禁售菜品
     */
    @PostMapping("/status/{status}")
    @ApiOperation("停售/禁售菜品")
    public Result updateStatus(@PathVariable Integer status,Long id) {
        log.info("停售/禁售菜品");
        dishService.updateStatus(id,status);
        cleanCache("dish_*");
        return Result.success();
    }

    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
